//
//  NumCell.m
//  HDYS
//
//  Created by kaynewbie on 15/4/24.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "NumCell.h"

@interface NumCell ()

@property (nonatomic, copy) NSString *numCount;

@property (nonatomic, strong) UILabel *showNumLabel;

@end

@implementation NumCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.numCount = @"1";
        [self createSubviews];
    }
    return self;
}

-(void)createSubviews{
    UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 20)];
    numLabel.text = @"数量";
    numLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:numLabel];
    
    UIButton *decreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    decreBtn.frame = CGRectMake(numLabel.frame.origin.x, numLabel.frame.origin.y + numLabel.frame.size.height + 10, 40, 30);
    [decreBtn setTitle:@"减" forState:UIControlStateNormal];
    [decreBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [decreBtn setBackgroundImage:[UIImage imageNamed:@"btn_confirm_87x36@2x"] forState:UIControlStateNormal];
    decreBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [decreBtn addTarget:self action:@selector(decreBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:decreBtn];
    
    UILabel *showNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(decreBtn.frame.origin.x + decreBtn.frame.size.width + 3, decreBtn.frame.origin.y , 60, decreBtn.frame.size.height)];
    showNumLabel.text = @"1";
    showNumLabel.textAlignment = NSTextAlignmentCenter;
    showNumLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:showNumLabel];
    self.showNumLabel = showNumLabel;
    
    UIButton *creBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    creBtn.frame = CGRectMake(showNumLabel.frame.origin.x + showNumLabel.frame.size.width + 3, decreBtn.frame.origin.y , decreBtn.frame.size.width, decreBtn.frame.size.height);
    [creBtn setTitle:@"增" forState:UIControlStateNormal];
    [creBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [creBtn setBackgroundImage:[UIImage imageNamed:@"btn_confirm_87x36@2x"] forState:UIControlStateNormal];
    creBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [creBtn addTarget:self action:@selector(creBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:creBtn];
}

- (void)decreBtnClick{
    int count = [self.numCount intValue];
    if (count == 1) {
        
    } else {
        self.numCount = [NSString stringWithFormat:@"%d", --count];
        self.showNumLabel.text = self.numCount;
    }
    [self numChanged];
}

- (void)creBtnClick{
    int count = [self.numCount intValue];
    self.numCount = [NSString stringWithFormat:@"%d",++count];
    self.showNumLabel.text = self.numCount;
    
    [self numChanged];
}

- (void)numChanged {
    if ([self.delegate respondsToSelector:@selector(chooseNum:)]) {
        [self.delegate chooseNum:self.numCount];
    }
}

@end
